package com.sphd.emanu.fragment.goodorder;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.goodorderdetail.GoodorderdetailFragment;
import com.sphd.emanu.interfaces.OrderCountCallBack;
import com.sphd.emanu.interfaces.SwfInterface;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.order.OrderCount;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 商品订单
 *
 * @Author Hu
 * @Time 2017/4/18 -- 10:37.
 * @Email 87683202@qq.com
 */

public class GoodorderFragment extends MVPBaseFragment<GoodorderContract.View, GoodorderPresenter> implements GoodorderContract.View, OrderCountCallBack, SwfInterface {

    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    Unbinder unbinder;
    MyViewPagerAdapter myadapter;
    List<Fragment> fragments;
    String[] titles = new String[]{"已支付\n0", "已接收\n0", "派送中\n0",
            "已完成\n0", "已取消\n0"};
    Bundle bundle;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_viewpager_tab;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();

        setUserVisibleHint(true);
    }

    @Override
    protected void initData() {
        initAdapter();
    }

    private void initAdapter() {
        fragments = new ArrayList<>();
        /**
         status订单状态,0-订单取消,1-订单提交,2-订单接受,3-订单拒绝,4-订单派送,5-订单完成,9-过期订单
         原型上的状态顺序为12450
         原型上的取消按钮变为拒绝,
         修改订单状态时,要变成啥状态就传对应的状态值,比如,要接受,就传2,要取消,就传0
         */
        for (int i = 0; i < 5; i++) {
            GoodorderdetailFragment goodorderdetailFragment = new GoodorderdetailFragment();
            goodorderdetailFragment.setType(i, getType(i));
            goodorderdetailFragment.setArguments(bundle);
            goodorderdetailFragment.setOrderCountCallBack(this);
            goodorderdetailFragment.setSwfInterface(this);
            fragments.add(goodorderdetailFragment);
        }

        myadapter = new MyViewPagerAdapter(context, getChildFragmentManager(), fragments, titles);
        myVp.setAdapter(myadapter);
        tabs.setupWithViewPager(myVp);
    }

    private int getType(int i) {
        int type = 0;
        switch (i) {
            case 0:
                type = 1;
                break;
            case 1:
                type = 2;
                break;
            case 2:
                type = 4;
                break;
            case 3:
                type = 5;
                break;
            case 4:
                type = 0;
                break;
        }
        return type;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void callBackOrderCount(OrderCount orderCount) {

        titles[0] = "已支付\n" + orderCount.getPayCount();
        titles[1] = "已接收\n" + orderCount.getAcceptCount();
        titles[2] = "派送中\n" + orderCount.getSendCount();
        titles[3] = "已完成\n" + orderCount.getFinishCount();
        titles[4] = "已取消\n" + orderCount.getCancelCount();
        myadapter.notifyDataSetChanged();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    

    @Override
    public void swf(int pos) {
        L.e(pos+"-=-=-=-=-=-=-=-=");
        myVp.setCurrentItem(pos);
        GoodorderdetailFragment goodorderdetailFragment = (GoodorderdetailFragment) fragments.get(pos);
        goodorderdetailFragment.onRefresh();
    }
}
